require 'rubygems'
require 'sequel'

class MoveConlumnCityFromPositionsToLocations < Sequel::Migration
    def up
        city_name_by_position_id = {}
        
        DB[:positions].each do |r|
           city_name_by_position_id[r[:id]] = r[:city]
        end

        city_name_by_position_id.each do |key, value|
           DB[:locations].insert(:position_id => key, :location => value)	
	end

        alter_table :positions do
            drop_column :city
        end
    end

    def down
        alter_table :positions do
            add_column :city, :varchar, :size =>128
        end

        city_name_by_position_id = {}
        
        DB[:locations].each do |r|
           city_name_by_position_id[r[:position_id]] = r[:location]
        end

        city_name_by_position_id.each do |key, value|       
            DB[:positions].filter(:id => key).update(:city => value.to_s)	
	end
    end
end
